layui.use(['table', 'admin', 'ax', 'func','form','laydate'], function () {
    var $ = layui.$;
    var table = layui.table;
    var $ax = layui.ax;
    var admin = layui.admin;
    var func = layui.func;
    var form = layui.form;
    var laydate = layui.laydate;

    //渲染时间选择框
    laydate.render({
        elem: '#zdrq',
        trigger: 'click',
        // max: Feng.currentDate()
    });

    /**
     * 管理
     */
    var BtyPsd = {
        tableId: "btyPsdTable"
    };
    form.on('select(type)', function (data) {
        BtyPsd.search();
    });

    /**
     * 初始化表格的列
     */
    BtyPsd.initColumn = function () {
        return [[
            // {type: 'checkbox'},
            {field: 'id', hide: true, title: ''},
            {field: 'ccustomerid', sort: true, title: '客户id'},
            {field: 'customerCode', sort: true, title: '客户编码'},
            {field: 'customerName', sort: true, title: '客户名称'},
            {field: 'ninnum', sort: true, title: '实入数量'},
            {field: 'noutnum', sort: true, title: '实出数量'},
            {field: 'nprice', sort: true, title: '单价'},
            {field: 'nmny', sort: true, title: '金额'},
            {field: 'vuserdef4', sort: true, title: '自定义项4'},
            {field: 'dbizdate', sort: true, title: '业务日期'},
            {field: 'cdispatcherid', sort: true, title: '收发类别ID'},
            {field: 'dispatcherCode', sort: true, title: '收发类别编码'},
            {field: 'dispatcherName', sort: true, title: '收发类别名称'},
            {field: 'cdptid', sort: true, title: '部门ID'},
            {field: 'deptCode', sort: true, title: '部门编号'},
            {field: 'deptName', sort: true, title: '部门名称'},
            {field: 'coperatorid', sort: true, title: '制单人ID'},
            {field: 'operatorCode', sort: true, title: '制单人编码'},
            {field: 'operatorName', sort: true, title: '制单人名称'},
            {field: 'cwarehouseid', sort: true, title: '仓库ID'},
            {field: 'warehouseidCode', sort: true, title: '仓库编号'},
            {field: 'warehouseidName', sort: true, title: '仓库名称'},
            {field: 'pkCalbody', sort: true, title: '库存组织PK'},
            {field: 'calbodyCode', sort: true, title: '库存组织编码'},
            {field: 'calbodyName', sort: true, title: '库存组织名称'},
            {field: 'pkCorp', sort: true, title: '公司ID'},
            {field: 'corpCode', sort: true, title: '公司编码'},
            {field: 'corpName', sort: true, title: '公司名称'},
            {field: 'daccountdate', sort: true, title: '签字日期'},
            {field: 'taccounttime', sort: true, title: '签字时间'},
            {field: 'tmaketime', sort: true, title: '制单时间'},
            {field: 'cbilltypecode', sort: true, title: '库存单据类型编码'},
            {field: 'vnote', sort: true, title: '备注'},
            {field: 'vbillcode', sort: true, title: '单据号'},
            {field: 'dbilldate', sort: true, title: '单据日期'},
            {field: 'cinventoryid', sort: true, title: '存货ID'},
            {field: 'cbodywarehouseid', sort: true, title: '库存仓库ID'},
            {field: 'bodywarehouseCode', sort: true, title: '库存仓库编码'},
            {field: 'bodywarehouseName', sort: true, title: '库存仓库名称'},


            {field: 'crowno', sort: true, title: '行号'},
            {field: 'nbarcodenum', sort: true, title: '条码数量'},

            {field: 'inventoryCode', sort: true, title: '存货编码'},
            {field: 'inventoryName', sort: true, title: '存货名称'},
            {field: 'invspec', sort: true, title: '规格'},
            {field: 'cvendorid', sort: true, title: '供应商ID'},
            {field: 'vendorCode', sort: true, title: '供应商编码'},
            {field: 'vendorName', sort: true, title: '供应商名称'},
            // {align: 'center', toolbar: '#tableBar', title: '操作'}
        ]];
    };

    /**
     * 点击查询按钮
     */
    BtyPsd.search = function () {
        var queryData = {};

        queryData['dispatcherName'] = $('#dispatcherName').val();
        queryData['deptName'] = $('#deptName').val();
        queryData['warehouseidName'] = $('#warehouseidName').val();
        queryData['corpName'] = $('#corpName').val();
        queryData['cbilltypecode'] = $("#type  option:selected").val();

        table.reload(BtyPsd.tableId, {
            where: queryData, page: {curr: 1}
        });
    };

    $('#btnPz').click(function () {

        BtyPsd.confirmOk();

    });

    BtyPsd.confirmOk = function () {
        var type=$("#type").find("option:selected").val();
        if(type==""){
            Feng.error("请选择单据类型！");
            return;
        }

         var timeLimit=$("#zdrq").val();
        if(timeLimit==""){
            Feng.error("请选择制单日期！");
            return;
        }
        var ajax = new $ax(Feng.ctxPath + "/btyPsd/confirmOk", function (data) {
            if(data.code==200){
                Feng.success("凭证生成成功!");
            }else {
                alert(data.message);
            }
            table.reload(BtyPsd.tableId);
        }, function (data) {
            Feng.error("生成失败!" + data.responseJSON.message + "!");
        });
        ajax.set("zdrq", timeLimit);
        // ajax.set("dm", dm);
        // ajax.set("yaoma", yaoma);
        ajax.set("cbilltypecode", type);
        ajax.start();
    };


    /**
     * 弹出添加对话框
     */
    BtyPsd.openAddDlg = function () {
        func.open({
            title: '添加',
            content: Feng.ctxPath + '/btyPsd/add',
            tableId: BtyPsd.tableId
        });
    };

     /**
      * 点击编辑
      *
      * @param data 点击按钮时候的行数据
      */
      BtyPsd.openEditDlg = function (data) {
          func.open({
              title: '修改',
              content: Feng.ctxPath + '/btyPsd/edit?id=' + data.id,
              tableId: BtyPsd.tableId
          });
      };


    /**
     * 导出excel按钮
     */
    BtyPsd.exportExcel = function () {
        var checkRows = table.checkStatus(BtyPsd.tableId);
        if (checkRows.data.length === 0) {
            Feng.error("请选择要导出的数据");
        } else {
            table.exportFile(tableResult.config.id, checkRows.data, 'xls');
        }
    };

    /**
     * 点击删除
     *
     * @param data 点击按钮时候的行数据
     */
    BtyPsd.onDeleteItem = function (data) {
        var operation = function () {
            var ajax = new $ax(Feng.ctxPath + "/btyPsd/delete", function (data) {
                Feng.success("删除成功!");
                table.reload(BtyPsd.tableId);
            }, function (data) {
                Feng.error("删除失败!" + data.responseJSON.message + "!");
            });
            ajax.set("id", data.id);
            ajax.start();
        };
        Feng.confirm("是否删除?", operation);
    };

    // 渲染表格
    var tableResult = table.render({
        elem: '#' + BtyPsd.tableId,
        url: Feng.ctxPath + '/btyPsd/list',
        page: true,
        height: "full-158",
        cellMinWidth: 100,
        cols: BtyPsd.initColumn()
    });

    // 搜索按钮点击事件
    $('#btnSearch').click(function () {
        BtyPsd.search();
    });

    // 添加按钮点击事件
    $('#btnAdd').click(function () {

    BtyPsd.openAddDlg();

    });

    // 导出excel
    $('#btnExp').click(function () {
        BtyPsd.exportExcel();
    });

    // 工具条点击事件
    table.on('tool(' + BtyPsd.tableId + ')', function (obj) {
        var data = obj.data;
        var layEvent = obj.event;

        if (layEvent === 'edit') {
            BtyPsd.openEditDlg(data);
        } else if (layEvent === 'delete') {
            BtyPsd.onDeleteItem(data);
        }
    });
});
